//
// MATLAB Compiler: 4.11 (R2009b)
// Date: Thu Oct 21 20:57:50 2010
// Arguments: "-B" "macro_default" "-C" "-W" "cpplib:libfilter" "-T" "link:lib"
// "initialize_x_and_p" "ekf_filter" "GenerateObs" "ekf_prediction"
// "ekf_update_vw" 
//

#include <stdio.h>
#define EXPORTING_libfilter 1
#include "libfilter.h"
#ifdef __cplusplus
extern "C" {
#endif

extern mclComponentData __MCC_libfilter_component_data;

#ifdef __cplusplus
}
#endif


static HMCRINSTANCE _mcr_inst = NULL;


#if defined( _MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__LCC__)
#ifdef __LCC__
#undef EXTERN_C
#endif
#include <windows.h>

static char path_to_dll[_MAX_PATH];

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void *pv)
{
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        if (GetModuleFileName(hInstance, path_to_dll, _MAX_PATH) == 0)
            return FALSE;
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
    }
    return TRUE;
}
#endif
#ifdef __cplusplus
extern "C" {
#endif

static int mclDefaultPrintHandler(const char *s)
{
  return mclWrite(1 /* stdout */, s, sizeof(char)*strlen(s));
}

#ifdef __cplusplus
} /* End extern "C" block */
#endif

#ifdef __cplusplus
extern "C" {
#endif

static int mclDefaultErrorHandler(const char *s)
{
  int written = 0;
  size_t len = 0;
  len = strlen(s);
  written = mclWrite(2 /* stderr */, s, sizeof(char)*len);
  if (len > 0 && s[ len-1 ] != '\n')
    written += mclWrite(2 /* stderr */, "\n", sizeof(char));
  return written;
}

#ifdef __cplusplus
} /* End extern "C" block */
#endif

/* This symbol is defined in shared libraries. Define it here
 * (to nothing) in case this isn't a shared library. 
 */
#ifndef LIB_libfilter_C_API
#define LIB_libfilter_C_API /* No special import/export declaration */
#endif

LIB_libfilter_C_API 
bool MW_CALL_CONV libfilterInitializeWithHandlers(
    mclOutputHandlerFcn error_handler,
    mclOutputHandlerFcn print_handler)
{
  if (_mcr_inst != NULL)
    return true;
  if (!mclmcrInitialize())
    return false;
  if (!GetModuleFileName(GetModuleHandle("libfilter"), path_to_dll, _MAX_PATH))
    return false;
  if (!mclInitializeComponentInstance(&_mcr_inst, &__MCC_libfilter_component_data, true, 
                                      NoObjectType, LibTarget, error_handler, 
                                      print_handler))
    return false;
  return true;
}

LIB_libfilter_C_API 
bool MW_CALL_CONV libfilterInitialize(void)
{
  return libfilterInitializeWithHandlers(mclDefaultErrorHandler, mclDefaultPrintHandler);
}

LIB_libfilter_C_API 
void MW_CALL_CONV libfilterTerminate(void)
{
  if (_mcr_inst != NULL)
    mclTerminateInstance(&_mcr_inst);
}

LIB_libfilter_C_API 
long MW_CALL_CONV libfilterGetMcrID() 
{
  return mclGetID(_mcr_inst);
}

LIB_libfilter_C_API 
void MW_CALL_CONV libfilterPrintStackTrace(void) 
{
  char** stackTrace;
  int stackDepth = mclGetStackTrace(_mcr_inst, &stackTrace);
  int i;
  for(i=0; i<stackDepth; i++)
  {
    mclWrite(2 /* stderr */, stackTrace[i], sizeof(char)*strlen(stackTrace[i]));
    mclWrite(2 /* stderr */, "\n", sizeof(char)*strlen("\n"));
  }
  mclFreeStackTrace(&stackTrace, stackDepth);
}


LIB_libfilter_C_API 
bool MW_CALL_CONV mlxInitialize_x_and_p(int nlhs, mxArray *plhs[], int nrhs, mxArray 
                                        *prhs[])
{
  return mclFeval(_mcr_inst, "initialize_x_and_p", nlhs, plhs, nrhs, prhs);
}

LIB_libfilter_C_API 
bool MW_CALL_CONV mlxEkf_filter(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "ekf_filter", nlhs, plhs, nrhs, prhs);
}

LIB_libfilter_C_API 
bool MW_CALL_CONV mlxGenerateObs(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "GenerateObs", nlhs, plhs, nrhs, prhs);
}

LIB_libfilter_C_API 
bool MW_CALL_CONV mlxEkf_prediction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "ekf_prediction", nlhs, plhs, nrhs, prhs);
}

LIB_libfilter_C_API 
bool MW_CALL_CONV mlxEkf_update_vw(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "ekf_update_vw", nlhs, plhs, nrhs, prhs);
}

LIB_libfilter_CPP_API 
void MW_CALL_CONV initialize_x_and_p(int nargout, mwArray& x_k_k, mwArray& p_k_k, 
                                     mwArray& filter, const mwArray& v_0, const mwArray& 
                                     std_v_0, const mwArray& w_0, const mwArray& std_w_0)
{
  mclcppMlfFeval(_mcr_inst, "initialize_x_and_p", nargout, 3, 4, &x_k_k, &p_k_k, &filter, &v_0, &std_v_0, &w_0, &std_w_0);
}

LIB_libfilter_CPP_API 
void MW_CALL_CONV ekf_filter(int nargout, mwArray& f, const mwArray& x_k_k, const 
                             mwArray& p_k_k, const mwArray& sigma_a, const mwArray& 
                             sigma_alpha, const mwArray& sigma_imn, const mwArray& type)
{
  mclcppMlfFeval(_mcr_inst, "ekf_filter", nargout, 1, 6, &f, &x_k_k, &p_k_k, &sigma_a, &sigma_alpha, &sigma_imn, &type);
}

LIB_libfilter_CPP_API 
void MW_CALL_CONV GenerateObs(int nargout, mwArray& xr_k, mwArray& z_k, const mwArray& 
                              xr_k_in1, const mwArray& delta_t, const mwArray& type, 
                              const mwArray& std_a, const mwArray& std_alpha, const 
                              mwArray& std_vw)
{
  mclcppMlfFeval(_mcr_inst, "GenerateObs", nargout, 2, 6, &xr_k, &z_k, &xr_k_in1, &delta_t, &type, &std_a, &std_alpha, &std_vw);
}

LIB_libfilter_CPP_API 
void MW_CALL_CONV ekf_prediction(int nargout, mwArray& f, const mwArray& f_in1, const 
                                 mwArray& delta_t)
{
  mclcppMlfFeval(_mcr_inst, "ekf_prediction", nargout, 1, 2, &f, &f_in1, &delta_t);
}

LIB_libfilter_CPP_API 
void MW_CALL_CONV ekf_update_vw(int nargout, mwArray& filter, const mwArray& filter_in1, 
                                const mwArray& v, const mwArray& w, const mwArray& std_vw)
{
  mclcppMlfFeval(_mcr_inst, "ekf_update_vw", nargout, 1, 4, &filter, &filter_in1, &v, &w, &std_vw);
}
